package ld.oauth.blog.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.ArrayList;
import java.util.List;

public class PageUtil {
    public static <T> IPage<T> createIPage(PageParam pageParam) {
        IPage<T> page = new PageResult<>();
        page.setCurrent(pageParam.getPageNum());
        page.setSize(pageParam.getPageSize());
        return page;
    }

    public static <T> PageResult<T> createPageResult(IPage page) {
        PageResult<T> pageResult = new PageResult<>();
        pageResult.setCurrent(page.getCurrent());
        pageResult.setSize(page.getSize());
        pageResult.setRecords(new ArrayList<>(0));
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    public static <T> PageResult<T> createPageResult(IPage page, List<T> records) {
        PageResult<T> pageResult = new PageResult<>();
        pageResult.setCurrent(page.getCurrent());
        pageResult.setSize(page.getSize());
        pageResult.setRecords(records);
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    public static <T, S> PageStatisticsResult<T, S> createPageStatisticsResult(IPage page,
                                                                               S statisticsData) {
        PageStatisticsResult<T, S> pageStatisticsResult = new PageStatisticsResult<>();
        pageStatisticsResult.setCurrent(page.getCurrent());
        pageStatisticsResult.setSize(page.getSize());
        pageStatisticsResult.setRecords(new ArrayList<>(0));
        pageStatisticsResult.setTotal(page.getTotal());
        pageStatisticsResult.setStatisticsData(statisticsData);
        return pageStatisticsResult;
    }

    public static <T, S> PageStatisticsResult<T, S> createPageStatisticsResult(IPage page, List<T> records, S statisticsData) {
        PageStatisticsResult<T, S> pageStatisticsResult = new PageStatisticsResult<>();
        pageStatisticsResult.setCurrent(page.getCurrent());
        pageStatisticsResult.setSize(page.getSize());
        pageStatisticsResult.setRecords(records);
        pageStatisticsResult.setTotal(page.getTotal());
        pageStatisticsResult.setStatisticsData(statisticsData);
        return pageStatisticsResult;
    }
}
